#include "gamescore.h"
#include <QKeyEvent>
#include <QFont>
#include <QBrush>
#include <QImage>

GameScore::GameScore(int score, QObject *parent)
    : QGraphicsScene(parent)
{
    setSceneRect(0, 0, 800, 400);
    setupScoreScreen(score);
}

void GameScore::setupScoreScreen(int score)
{
    // 背景
    setBackgroundBrush(QBrush(QImage(":/image/D:/score_bg.jpg").scaled(800, 400)));

    // 游戏结束文本
    QGraphicsTextItem *gameOverText = new QGraphicsTextItem("游戏结束");
    QFont gameOverFont("Arial", 36, QFont::Bold);
    gameOverText->setFont(gameOverFont);
    gameOverText->setDefaultTextColor(Qt::red);
    int xPos = width()/2 - gameOverText->boundingRect().width()/2;
    gameOverText->setPos(xPos, 50);
    addItem(gameOverText);

    // 分数显示
    QGraphicsTextItem *scoreText = new QGraphicsTextItem(QString("得分: %1").arg(score));
    QFont scoreFont("Arial", 28);
    scoreText->setFont(scoreFont);
    scoreText->setDefaultTextColor(Qt::white);
    xPos = width()/2 - scoreText->boundingRect().width()/2;
    scoreText->setPos(xPos, 150);
    addItem(scoreText);

    // 重新开始文本
    QGraphicsTextItem *restartText = new QGraphicsTextItem("重新开始 (按R键)");
    QFont restartFont("Arial", 24);
    restartText->setFont(restartFont);
    restartText->setDefaultTextColor(Qt::white);
    xPos = width()/2 - restartText->boundingRect().width()/2;
    restartText->setPos(xPos, 250);
    addItem(restartText);

    // 返回菜单文本
    QGraphicsTextItem *menuText = new QGraphicsTextItem("返回菜单 (按M键)");
    QFont menuFont("Arial", 24);
    menuText->setFont(menuFont);
    menuText->setDefaultTextColor(Qt::white);
    xPos = width()/2 - menuText->boundingRect().width()/2;
    menuText->setPos(xPos, 300);
    addItem(menuText);
}

void GameScore::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()) {
    case Qt::Key_R:
        emit restartGame();
        break;
    case Qt::Key_M:
        emit returnToMenu();
        break;
    }
}
